package com.servlet;

import com.dao.ProductDAO;
import com.dao.impl.ProductDAOImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/*
 *
 *@author XCH
 *@date2024 - 11 - 28
 *
 */
@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        System.out.println(id);
        ProductDAO dao = new ProductDAOImpl();
        int result = dao.deleteProduct(id);
        System.out.println(result);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if (result > 0) {
            out.print("{\"code\":200, \"message\":\"商品删除成功\"}");
        } else {
            out.print("{\"code\":500, \"message\":\"商品删除失败\"}");
        }
    }
}
